#include "tuya.h"
#include "wifi.h"
#include <stdbool.h>
#include "APP_Main.h"
#include "DP_Param.h"


uint8_t Uart_TuYa_TxComplete = false;

#define WIFI_Config_Power_Delay 425 //850ms上电延迟后才能配网(第一个心跳包之后)
#define WIFI_Config_TimeoutCount 500  //5s超时后自动重新配置

WIFI_Config_E WIFI_Config = WIFI_Config_Disable;
uint16_t WIFI_Config_Timeout = 0;
uint8_t StateOf_WIFI = WIFI_NOT_CONNECTED;
uint16_t ShineTime_WiFi_Flag = 0;

App_Params_S App_Params;
uint16_t WIFI_Timeout = 0xffff; //Tick 10ms
uint8_t WiFi_IsReady = 0;

uint16_t OTA_Count = 0;
bool OTA_Animain_Enable = false;
uint8_t OTA_UpdateFlag = 0;

uint8_t Trigger_WiFiTest = 0;


void OTA_Cancel()
{
	OTA_Count = 0;
	OTA_Animain_Enable = false;
}
void OTA_Start(void)
{
    OTA_Count = 30000; //Tick 2ms
}
bool OTA_IsActive(void)
{
    if(OTA_Count > 0)
	{
		OTA_Animain_Enable = true;
		return true;
	}
    else
	{
		OTA_Cancel();	
		return false; 
	}	
}

//Tick 10ms
void TuYa_Clock(void)
{
	if((WIFI_Config_Timeout > 0) && (WIFI_Config_Timeout < 0xffff)) WIFI_Config_Timeout++;
	if((WIFI_Timeout > 0) && (WIFI_Timeout < 0xffff)) WIFI_Timeout++;
	if(OTA_Count > 0) OTA_Count--;
	DP_Param_Clock();
}

//Tick 500ms
void TuYa_Param_TxClock(void)
{
	static uint8_t count = 0;

	count++;
	if(count >= 1)
	{ //Tick 500ms
		count = 0;

		// if((OTA_IsActive() == false) && (WIFI_Config == WIFI_Config_Disable)) all_data_update();
	}
}

void TuYa_Init(void)
{
	GPIO_Mode_Uart_Tx();
	GPIO_Mode_Uart_Rx();
	UART0_CONFIG();
	
	delay_nms(100);
	wifi_protocol_init();

	// App_Params.Mode_CH = false; //上电默认关闭 CH
	// App_Params.Mode_DHW = State_Not; //上电默认DHW 无效
	// // App_Params.CurRunMode = RunMode_D1_ON;
	// App_Params.AutoMode = AutoMode_P5;
	// DP_Param_Init();	

	DP_Param_Init();
}
void TuYa_WiFi_ConfigNet(void)
{
	if(WIFI_Config != WIFI_Config_Smart)	
	{
		WIFI_Config = WIFI_Config_Smart;
		WIFI_Timeout = 0;

		UI_System.ICO_WIFI = UI_Shine;
	}	
}
void TuYa_WiFi_ConfigNetCancel()
{
	WIFI_Config = WIFI_Config_Disable;
	WIFI_Timeout = 0;
}
void TuYa_isReadlyOK()
{
	WIFI_Timeout = 0;
    WiFi_IsReady = 1;
}
void TuYa_Main(void)
{
	wifi_uart_service();
	if(WiFi_IsReady > 0)
	{
		if(Trigger_WiFiTest > 0)
		{ //wifi产测
			Trigger_WiFiTest = 0;
			
			mcu_start_wifitest();
		}
		if(WIFI_Config == WIFI_Config_Smart)
		{ //配网模式
			if(WIFI_Timeout == 0)
			{
				WIFI_Timeout = 1;
				mcu_set_wifi_mode(WIFI_Config);
			}
			else
			{
				if(mcu_get_wifimode_flag() == SET_WIFICONFIG_ERROR)
				{
					if(WIFI_Timeout > WIFI_Config_TimeoutCount) WIFI_Timeout = 0;	
				}
				else
				{
					WIFI_Timeout = 0;
					WIFI_Config = WIFI_Config_Smarting;
				}
			}
		}
		else if(WIFI_Config == WIFI_Config_Smarting)
		{
			if(mcu_get_wifi_work_state() == WIFI_CONN_CLOUD)
			{ //配网成功, 取消配网过程
				TuYa_WiFi_ConfigNetCancel();
			}
			else
			{
				if((mcu_get_wifi_work_state() == SMART_CONFIG_STATE) || (mcu_get_wifi_work_state() == AP_STATE) || (mcu_get_wifi_work_state() == SMART_AND_AP_STATE))
				{ //正在配网过程
					UI_System.ICO_WIFI = UI_Shine;
				}
				if(WIFI_Timeout == 0) WIFI_Timeout = 1;
				else if(WIFI_Timeout >= (100 * 60 * 2))
				{ //2min配网超时
					TuYa_WiFi_ConfigNetCancel();
				}
			}	
		}
		else
		{ //非配网状态
			if(mcu_get_wifi_work_state() == WIFI_CONN_CLOUD)
			{ //已连接 服务器
				UI_System.ICO_WIFI = UI_ON;

				DP_Param_Main();
			}
			else if((mcu_get_wifi_work_state() == SMART_CONFIG_STATE) || (mcu_get_wifi_work_state() == AP_STATE) || (mcu_get_wifi_work_state() == SMART_AND_AP_STATE))
			{ //被动配网状态
				UI_System.ICO_WIFI = UI_Shine;
			}
			else
			{ //离线
				UI_System.ICO_WIFI = UI_OFF;
			}
		}
	}
	else
	{
		if(WIFI_Timeout == 0) WIFI_Timeout = 1;
		else if(WIFI_Timeout >= (100 * 5))
		{
			WIFI_Timeout = 0;

			mcu_get_wifi_connect_status();
			// mcu_reset_wifi(); //根据手册提示 会导致模组重置为配网模式
		}
	}	
	
}

//Tick 2ms
void TuYa_WiFi_ShineClock(void)
{
	static uint16_t count = 0;

	DP_Param_Clock();
	if(count < ShineTime_WiFi_Flag)
	{
		count++;
		if(count == ShineTime_WiFi_Flag)
		{
			count = 0;
			// LED_DisBuf.Icon_WiFi = 1 - LED_DisBuf.Icon_WiFi;
		}
	}
	else if(count != 0) count = 0;
}

void TuYa_Uart_RxHandle(unsigned char value)
{
	uart_receive_input(value);
}
